/*
 * DBConnectionPool.java
 *
 * Created on July 2, 2007, 10:50 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.atomojo.app.db;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 *
 * @author alex
 */
public class DBConnectionPool
{
   
   List<DBConnection> connections;
   List<DBConnection> available;
   Properties dbProps;
   String url;
   int poolSize;
   protected String dbName;
   protected File dbDir;
   
   /** Creates a new instance of DBConnectionPool */
   public DBConnectionPool(String dbName,File dir)
   {
      dbProps = new Properties();
      this.dbDir = dir;
      url = dbName==null ? null : "jdbc:derby:"+dir.getAbsolutePath()+";create=true";
      connections = new ArrayList<DBConnection>();
      available = new ArrayList<DBConnection>();
      poolSize = 3;
      this.dbName = dbName;
   }
   
   public String getName() {
      return dbName;
   }
   
   public File getDatabaseDir() {
      return dbDir;
   }
   
   public void setConnection(String name,File dir)
   {
      dbName = name;
      this.dbDir = dir;
      url = dbName==null ? null : "jdbc:derby:"+dir.getAbsolutePath()+";create=true";
   }
   
   protected Connection getNonPooled() 
      throws SQLException
   {
      return DriverManager.getConnection(url,dbProps);
   }
   public DBConnection getConnection() 
      throws SQLException
   {
      DBConnection dbConnection = null;
      synchronized (available) {
         if (available.size()==0) {
            Connection connection = getNonPooled();
            dbConnection = new DBConnection(connection);
            prepare(dbConnection);
            connections.add(dbConnection);
         } else {
            dbConnection = available.remove(0);
         }
      }
      return dbConnection;
   }
   
   public void release(DBConnection c) {
      synchronized (available) {
         available.add(c);
      }
   }
   
   public Properties setDatabaseProperties() {
      return dbProps;
   }
   
   public void setDatabaseProperties(Properties dbProps) {
      this.dbProps = dbProps;
   }
   
   public String getURL() {
      return url;
   }
   
   public void setURL(String url) {
      this.url = url;
   }
   
   public void setPoolSize(int size)
   {
      poolSize = size;
   }
   
   public void initPool() 
      throws SQLException
   {
      for (int i=0; i<poolSize; i++) {
         Connection c = DriverManager.getConnection(url,dbProps);
         DBConnection dbConnection = new DBConnection(c);
         prepare(dbConnection);
         connections.add(dbConnection);
         available.add(dbConnection);
      }
   }
   
   public void close()
      throws SQLException
   {
      for (DBConnection dbConnection : connections) {
         dbConnection.close();
         dbConnection.getConnection().close();
      }
      connections.clear();
      available.clear();
   }
   
   protected void prepare(DBConnection connection) 
      throws SQLException
   {
   }
   
}
